// redis 链接
var redis   = require('redis');
var config  = require('./config.js'); 

function Zredis(){
	
	  
	 var client = redis.createClient(config.redis.port, config.redis.host);

	// redis 链接错误
	client.on("error", function(error) {
		console.log("+++++++++++++++++++");
		console.log(error);
		client.end();	
		client = redis.createClient(PORT, HOST);
	});

	// redis 验证 (reids.conf未开启验证，此项可不需要)
	client.auth(config.redis.password);

	function SetData(key,data,callback){
		
		client.select(config.redis.database, function(error){
				
			client.hmset(key, data, function(err, res){
				if(err){
					console.log(err);
				}else{
					
					//console.log('res:'+res);
					return callback(err,res);
										
				}
				
				// 关闭链接
				//client.end();
				
			});
			
		});
	}

	function GetData(key,callback){
		
		client.select(config.redis.database, function(error){
		
			// hgetall
			client.hgetall(key, function(error, res){
				
					console.log(res);
					return callback(error,res);
			});

			// 关闭链接
			//client.end();
			
		});
		
	}
	
	return {
		  SetData: SetData,
		  GetData: GetData
	   
	  };
}

module.exports = Zredis;


